<?php
class DigUser_AccountController extends Zend_Controller_Action {

    public function init() {
        DigUi_Service_Template::useAdminTemplate();
        parent::init();
    }
    public function indexAction() {
        $this->view->users = DigUser_Model_Account::listUsersByRole();
    }

    public function myAccountAction() {
        $this->view->identity = DigUser_Service_User::current ();
    }

    /**
     * this method is called via an AJAX request and returns the new user as
     * a JSON object
     */
    public function createAction()
    {
        $response = DigUser_Model_Account::createUser();
        $this->_helper->json($response);
    }

    public function formAction() {
        $this->_helper->viewRenderer->setNoRender();
        $form = new DigUser_Form_Account ( );
        $form->setAction('/digUser/account/save');
        $id = $this->getRequest()->getParam('id');
        if($id > 0) {
            $model = new DigUser_Model_Account();
            $user = $model->find($id)->current();
            $form->populate($user->toArray());
        }
        echo $form;
    }

    public function saveAction() {
        $this->_helper->viewRenderer->setNoRender();
        $form = new DigUser_Form_Account ( );
        $form->setAction('/digUser/account/save');
        if($form->validatePost()) {
            $this->view->digUiMessage()->addMessage( 'Your user was successfully updated.', DigUi_View_Helper_DigUiMessage::INFO );
            $model = new DigUser_Model_Account();
            $model->saveForm($form);
        }
        echo $form;
    }

    public function renameAction()
    {
        $id = $this->_request->getParam ( 'id' );
        $newName = $this->getRequest()->getParam('name');
        $user = DigUser_Model_Account::renameUser($id, $newName);
        if($user) {
            $response['user'] = $user->toArray();
        }
        $this->_helper->json($response);
    }

    public function setRoleAction() {
        $id = $this->_request->getParam ( 'id' );
        $role = $this->_request->getParam( 'role');
        $response = array();
        $response['success'] = DigUser_Model_Account::setRole($id, $role);
        $this->_helper->json($response);
    }

    public function deleteAction() {
        $id = $this->_request->getParam ( 'id' );
        $userModel = new DigUser_Model_Account ( );
        $result = $userModel->deleteById($id);
        return $this->_helper->json(array('result' => $result));
    }

    public function loginAction() {
        $userForm = new DigUser_Form_Account( );
        $userForm->setAction ( '/digUser/account/login' );
        $userForm->removeElement ( 'first_name' );
        $userForm->removeElement ( 'last_name' );
        $userForm->removeElement ( 'role' );
        if ($this->_request->isPost () && $userForm->isValid ( $_POST )) {
            $data = $userForm->getValues ();
            //set up the auth adapter
            // get the default db adapter
            $db = Zend_Db_Table::getDefaultAdapter ();
            //create the auth adapter
            $authAdapter = new Zend_Auth_Adapter_DbTable ( $db, 'users', 'username', 'password' );
            //set the username and password
            $authAdapter->setIdentity ( $data ['username'] );
            $authAdapter->setCredential ( md5 ( $data ['password'] ) );
            //authenticate
            $result = $authAdapter->authenticate ();
            if ($result->isValid ()) {
                // store the username, first and last names of the user
                $auth = Zend_Auth::getInstance ();
                $storage = $auth->getStorage ();
                $storage->write ( $authAdapter->getResultRowObject ( array ('id', 'username', 'first_name', 'last_name', 'role' ) ) );
                $this->_redirect('/dig');
            } else {
                $this->view->loginMessage = "Sorry, your username or
                password was incorrect";
            }
        }
        $this->view->form = $userForm;
    }
    public function logoutAction() {
        $authAdapter = Zend_Auth::getInstance ();
        $authAdapter->clearIdentity ();
    }

}

